﻿using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Xml;
using EMANIFEST.DataAccess.Models;
using EMANIFEST.Dtos;
using System.Collections.Generic;

namespace EMANIFEST.DataAccess
{
    public partial class BusinessDataAccess
    {
        Dto2Xml d2xml = new Dto2Xml();

        private enum enumFormXML
        {
            GoodDeclaration=1,
            HouseBill=2,
            GeneralDeclaration=3,
            Crew=4,
            CrewEffectDeclaration=5,
            ShipStoreDeclaration=6,
            Passenger=7,
            DangerousGood=8
        }

      

        public void ImportDangerousGoodXML(DangerousGoodDto dg, enumUploadType uploadType)
        {

            // Check document submited
            //dg.IsTemp = IsSubmitDocument(dg.DocumentId);

            //// Update document status
            //if (dg.IsTemp)
            //{
            //    SetDocumentStatusUpdate(dg.DocumentId, enumDocumentUpdate.DangerousGood);
            //}

            string chuoiXML = Dto2Xml.DangerousGood2XML(dg);
            var retVal = new System.Data.Objects.ObjectParameter("retVal", typeof(int));
            if (uploadType == enumUploadType.Addition || uploadType == enumUploadType.Original)
            {
                eManifestModel.SaveDocumentItemXml(dg.DocumentId, dg.CreaterId, chuoiXML, false, (int)enumFormXML.DangerousGood, retVal);
            }
            else if (uploadType == enumUploadType.Replace)
                eManifestModel.SaveDocumentItemXml(dg.DocumentId, dg.CreaterId, chuoiXML, true, (int)enumFormXML.DangerousGood, retVal);
            else
                return;
        }


        public void ImportHouseBillXML(HouseBillDto hb, enumUploadType uploadType)
        {

            // Check document submited
            //hb.IsTemp = IsSubmitDocument(hb.DocumentId);

            //// Update document status
            //if (hb.IsTemp)
            //{
            //    SetDocumentStatusUpdate(hb.DocumentId, enumDocumentUpdate.HouseBill);
            //}

            string chuoiXML = Dto2Xml.HouseBill2XML(hb);
            var retVal = new System.Data.Objects.ObjectParameter("retVal", typeof(int));
            if (uploadType == enumUploadType.Addition || uploadType == enumUploadType.Original)
                eManifestModel.SaveDocumentItemXml(hb.DocumentId, hb.CreaterId, chuoiXML, false, (int)enumFormXML.HouseBill, retVal);
            else if (uploadType == enumUploadType.Replace)
                eManifestModel.SaveDocumentItemXml(hb.DocumentId, hb.CreaterId, chuoiXML, true, (int)enumFormXML.HouseBill, retVal);
            else
                return;
        }


        public void ImportShipStoreDeclarationXML(ShipStoreDeclarationDto ssd, enumUploadType uploadType)
        {

            // Check document submited
            //hb.IsTemp = IsSubmitDocument(hb.DocumentId);

            //// Update document status
            //if (hb.IsTemp)
            //{
            //    SetDocumentStatusUpdate(hb.DocumentId, enumDocumentUpdate.HouseBill);
            //}

            string chuoiXML = Dto2Xml.ShipStoreDeclaration2XML(ssd);
            var retVal = new System.Data.Objects.ObjectParameter("retVal", typeof(int));
            if (uploadType == enumUploadType.Addition || uploadType == enumUploadType.Original)
                eManifestModel.SaveDocumentItemXml(ssd.DocumentId, ssd.CreaterId, chuoiXML, false, (int)enumFormXML.ShipStoreDeclaration, retVal);
            else if (uploadType == enumUploadType.Replace)
                eManifestModel.SaveDocumentItemXml(ssd.DocumentId, ssd.CreaterId, chuoiXML, true, (int)enumFormXML.ShipStoreDeclaration, retVal);
            else
                return;
        }


        public void ImportCrewEffectDeclarationXML(List<CrewEffectDeclarationDto> ced, enumUploadType uploadType)
        {
            // Check document submited
            //hb.IsTemp = IsSubmitDocument(hb.DocumentId);

            //// Update document status
            //if (hb.IsTemp)
            //{
            //    SetDocumentStatusUpdate(hb.DocumentId, enumDocumentUpdate.HouseBill);
            //}

            string chuoiXML = Dto2Xml.CrewEffect2XML(ced);
            var retVal = new System.Data.Objects.ObjectParameter("retVal", typeof(int));
            if (uploadType == enumUploadType.Addition || uploadType == enumUploadType.Original)
                eManifestModel.SaveDocumentItemXml(ced[0].DocumentId, ced[0].CreaterId, chuoiXML, false, (int)enumFormXML.CrewEffectDeclaration, retVal);
            else if (uploadType == enumUploadType.Replace)
                eManifestModel.SaveDocumentItemXml(ced[0].DocumentId, ced[0].CreaterId, chuoiXML, true, (int)enumFormXML.CrewEffectDeclaration, retVal);
            else
                return;
        }


        public void ImportPassengerXML(List<PassengerDto> p, enumUploadType uploadType)
        {
            // Check document submited
            //p.IsTemp = IsSubmitDocument(p.DocumentId);

            //// Update document status
            //if (p.IsTemp)
            //{
             //   SetDocumentStatusUpdate(p[0].DocumentId, enumDocumentUpdate.Passenger);
            //}

            string chuoiXML = Dto2Xml.Passenger2XML(p);
            var retVal = new System.Data.Objects.ObjectParameter("retVal", typeof(int));
            if (uploadType == enumUploadType.Addition || uploadType == enumUploadType.Original)
                eManifestModel.SaveDocumentItemXml(p[0].DocumentId, p[0].CreaterId, chuoiXML, false, (int)enumFormXML.Passenger, retVal);
            else if (uploadType == enumUploadType.Replace)
                eManifestModel.SaveDocumentItemXml(p[0].DocumentId, p[0].CreaterId, chuoiXML, true, (int)enumFormXML.Passenger, retVal);
            else
                return;
        }


        public void ImportGoodDeclarationXMLDetail(GoodDeclarationDto objGoodDeclarationDto, enumUploadType uploadType)
        {
            try
            {
                string xmlString = Dto2Xml.GoodDeclaration2XML(objGoodDeclarationDto);
                var retVal = new System.Data.Objects.ObjectParameter("retVal", typeof(int));

                if (uploadType == enumUploadType.Addition || uploadType == enumUploadType.Original)
                {
                    eManifestModel.SaveDocumentItemXml(objGoodDeclarationDto.DocumentId,
                                                        objGoodDeclarationDto.CreaterId,
                                                        xmlString,false,
                                                        (int)enumFormXML.GoodDeclaration,retVal);
                }
                else if (uploadType == enumUploadType.Replace)
                {
                    eManifestModel.SaveDocumentItemXml(objGoodDeclarationDto.DocumentId,
                                                        objGoodDeclarationDto.CreaterId,
                                                        xmlString,
                                                        true,
                                                        (int)enumFormXML.GoodDeclaration,
                                                        retVal);
                }
                else 
                    return;

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException.Message);
            }
        }


        public void ImportCrewXMLDetail(List<CrewDto> listCrewDto, enumUploadType uploadType)
        {
            try
            {
                Dto2Xml objDto2Xml = new Dto2Xml();

                string xmlString = Dto2Xml.Crew2XML(listCrewDto);
                var retVal = new System.Data.Objects.ObjectParameter("retVal", typeof(int));

                if (uploadType == enumUploadType.Addition || uploadType == enumUploadType.Original)
                {
                    eManifestModel.SaveDocumentItemXml(listCrewDto[0].DocumentId, listCrewDto[0].CreaterId, xmlString, false, (int)enumFormXML.Crew, retVal);

                }
                else if (uploadType == enumUploadType.Replace)
                    eManifestModel.SaveDocumentItemXml(listCrewDto[0].DocumentId, listCrewDto[0].CreaterId, xmlString, true, (int)enumFormXML.Crew, retVal);
                else
                    return;

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException.Message);
            }
        }


        public ResponseDto ImportEDIDataXML(Stream content, int formId)
        {
            ResponseDto ret = new ResponseDto() { IsSuccess = true, ErroeMessage = string.Empty };
            var retVal = new System.Data.Objects.ObjectParameter("retVal", typeof(int));
            retVal.Value = 0;
            using (StreamReader sr = new StreamReader(content, Encoding.Unicode))
            {
                string strContent = sr.ReadToEnd();
                //int? totalRowsNum = 0;
                eManifestModel.SaveDocumentDataXml(strContent, (int)formId, retVal);
                //if (retVal != null && retVal.Value != null)
                //    ret.ErroeMessage = ((int)retVal.Value).ToString();
            }

            return ret;
        }


    }
}
